//nginx代理跨域
//var API_PROXY = ''
var API_PROXY = 'https://bird.ioliu.cn/v1/?url='
//请求地址
var serverUrl = "http://lbw.mihuan-net.com/interface/UI"
//ajax请求的url
var actUrl = API_PROXY + serverUrl;
var emailAccount = "1182975090@qq.com"
//日期选择器
//设置当前日期的第二天为默认的保险生效时间显示格式为2017-02-21
var d = new Date();
var defaultDate = getDateStr(d)
console.log("默认日期"+defaultDate)
console.log("当前时间戳:",d.getTime())
var nowTimestap = d.getTime();
var preDate1 = defaultDate;
var preDate2 = defaultDate;
var preDate3 = defaultDate;
var preDate4 = defaultDate;
//日期选择
var picker = new Picker(document.querySelector('#datePicker1'), {
	format: 'YYYY-MM-DD',
	rows:"1",
	date: defaultDate,
	hide:function(){//弹框隐藏时判断所选日期是否晚于当前时间
		console.log($('#datePicker1').val())
		$("#iconMore").hide()//隐藏按钮
		//获取选择的日期
		var choseDateStr = $('#datePicker1').val();
		
		var choseDate = new Date(choseDateStr + ' 00:00:00:000')
		//若选择日期大于当前日期
		if((choseDate).getTime() > nowTimestap){
			toast("不能选择今天之后的日期哦")
			$("#datePicker1").val(preDate1)
		}else if((choseDate).getTime() < nowTimestap){	
			preDate1 = choseDateStr;
			//重新绘制柱形图
			var param = {
				"transCode":"L0016",
				"emailAccount":emailAccount,
				"showDay":choseDateStr
			}
			//容器，请求参数（重新绘制）
			getWaterFlowChart('barContainer1',param,choseDateStr,'#todayFlow');
		}
	},
});

//第二个日期选择器
var picker2 = new Picker(document.querySelector('#datePicker2'), {
	format: 'YYYY-MM-DD',
	rows:"1",
	date: defaultDate,
	hide:function(){//弹框隐藏时判断所选日期是否晚于当前时间
		console.log($('#datePicker2').val())
		//获取选择的日期
		//获取选择的日期
		var choseDateStr = $('#datePicker2').val();
		
		var choseDate = new Date(choseDateStr + ' 00:00:00:000')
		//若选择日期大于当前日期
		if((choseDate).getTime() > nowTimestap){
//			alert("不能选择今天之后的日期哦")
			toast("不能选择今天之后的日期哦")
			$("#datePicker2").val(preDate2)
		}else if((choseDate).getTime() < nowTimestap){
			preDate2 = choseDateStr;
			//重新绘制
			//重新绘制柱形图
			var param = {
				"transCode":"L0015",
				"emailAccount":emailAccount,
				"showEndDay":choseDateStr
			}
			//容器，请求参数（重新绘制）
			getPHChart('barContainer2',param,choseDateStr,'#PH');
		}
	},
});

var picker3 = new Picker(document.querySelector('#datePicker3'), {
	format: 'YYYY-MM-DD',
	rows:"1",
	date: defaultDate,
	hide:function(){//弹框隐藏时判断所选日期是否晚于当前时间
		console.log($('#datePicker3').val())
		//获取选择的日期
		var choseDateStr = $('#datePicker3').val()
		var choseDate = new Date( choseDateStr + ' 00:00:00:000')
		console.log(choseDate)
		//若选择日期大于当前日期
		if((choseDate).getTime() > nowTimestap){
//			alert("不能选择今天之后的日期哦")
			toast("不能选择今天之后的日期哦")
			$("#datePicker3").val(preDate3)
		}else if((choseDate).getTime() < nowTimestap){
			preDate3 = choseDateStr;
			//重新绘制图表
			var paramCaMg = {
				"transCode":"L0013",
				"emailAccount":emailAccount,
				"showEndDay":choseDateStr,//默认日期（当前）
			}
			getCaMgChart('lineContainer',paramCaMg,choseDateStr)
		}
	},
});

//日期选择
var picker4 = new Picker(document.querySelector('#datePicker4'), {
	format: 'YYYY-MM-DD',
	rows:"1",
	date: defaultDate,
	hide:function(){//弹框隐藏时判断所选日期是否晚于当前时间
		console.log($('#datePicker4').val())
		$("#iconMore1").hide()//隐藏按钮
		//获取选择的日期
		var choseDateStr = $('#datePicker4').val();
		var choseDate = new Date(choseDateStr + ' 00:00:00:000')
		//若选择日期大于当前日期
		if((choseDate).getTime() > nowTimestap){
			toast("不能选择今天之后的日期哦")
			$("#datePicker4").val(preDate4)
		}else if((choseDate).getTime() < nowTimestap){	
			preDate4 = choseDateStr;
			var paramGuoLv = {
				"transCode":"L0014",
				"emailAccount":emailAccount,
				"showDay":choseDateStr
			}
			getGuoLvChart(paramGuoLv)  //渲染所有环 
		}
	},
});
//设置日期显示为当前日期
$("#datePicker2").val(defaultDate)
$("#datePicker3").val(defaultDate)

//用水习惯评分
$.ajax({
	type:"post",
	url:actUrl,
	async:true,
	contentType:"application/x-www-form-urlencoded;charset=UTF-8",
	data:{
		"transCode":"L0012",
		"emailAccount":emailAccount,
	},
	success:function(res){
		console.log("用水习惯评分",res)
		if(res.code==0){
			var data = res.data;
			var waterScore = parseInt(data.waterScore);//综合评分
			var waterRetrench = parseInt(data.waterRetrench);//节约用水
			var waterRule = parseInt(data.waterRule);//饮水规律
			var waterTransposing = parseInt(data.waterTransposing);//滤芯更换
			var waterHealthy = parseInt(data.waterHealthy);//用水健康
			var bot = (waterScore*1.7/100) + 'rem';//遮罩距离底部
			console.log(bot)
			$('#score').text(waterScore+"分")//分数显示
			$('#mask').css({'bottom':bot})//bot
			
			$('#data1').text(waterRetrench+'%')//waterRetrench
			$('#conSave').css('width',waterRetrench+'%')//waterRetrench/总值*100
			$('#data2').text(waterRule+'%')//waterRule
			$('#conRagu').css('width',waterRule+'%')
			$('#data3').text(waterTransposing+'%')//waterTransposing
			$('#conCha').css('width',waterTransposing+'%')
			$('#data4').text(waterHealthy+'%')//waterHealthy
			$('#conHealth').css('width',waterHealthy+'%')
		}
		
	},
	error:function(err){
		console.log("error",err)
	}
});

//CircleChart('containerRed','#ff2256','#f8b62d')
//CircleChart('containerGreen','#04a398','#edea07')
//CircleChart('containerBlue','#18a8e6','#323692')
//过滤重金属

//按日期查看PH

//页面刚加载的日流量-柱形图
var param = {
	"transCode":"L0016",
	"emailAccount":emailAccount,
	"showDay":defaultDate
}
getWaterFlowChart('barContainer1',param,defaultDate,'#todayFlow');

//页面加载的PH柱形图
var paramPH = {
	"transCode":"L0015",
	"emailAccount":emailAccount,
	"showEndDay":defaultDate
}
getPHChart('barContainer2',paramPH,defaultDate,'#PH');

var paramCaMg = {
	"transCode":"L0013",
	"emailAccount":emailAccount,
	"showEndDay":defaultDate,//默认日期（当前）
}
getCaMgChart('lineContainer',paramCaMg,defaultDate)

var paramGuoLv = {
	"transCode":"L0014",
	"emailAccount":emailAccount,
	"showDay":defaultDate
}
getGuoLvChart(paramGuoLv)  //渲染所有环 
//柱形图(日流量)
var dataArr = [0, 0, 0, 0, 0,0,0]
barChart("barContainer1",getDateArr(defaultDate,7),dataArr)
//PH柱形图
var dataPH = [7.0,7.0,7.0,7.0,7.0,7.0,7.0];
barChart("barContainer2",getDateArr(defaultDate,7),dataPH)
//Mg硬度Ca硬度
var data1 = [10,12,20,45,60,120,80];
var data2 = [45,60,100,80,70,30,50];
lineChart('lineContainer',getDateArr(defaultDate,7),data1,data2)


